<?php
/**
 * LBN Payment Gateway Online Payment PHP Demo
 * @author langs
 * @version demo
 */

require_once("config.php");
require_once("lib/submit.class.php");

function getCustormerIpaddress()
{
    if (isset ($_SERVER)) {
        if (isset ($_SERVER ['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER ['HTTP_X_FORWARDED_FOR']);

            foreach ($arr as $ip) {
                $ip = trim($ip);

                if ($ip != 'unknown') {
                    $realip = $ip;

                    break;
                }
            }
        } elseif (isset ($_SERVER ['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER ['HTTP_CLIENT_IP'];
        } else {
            if (isset ($_SERVER ['REMOTE_ADDR'])) {
                $realip = $_SERVER ['REMOTE_ADDR'];
            } else {
                $realip = '0.0.0.0';
            }
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $realip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('HTTP_CLIENT_IP')) {
            $realip = getenv('HTTP_CLIENT_IP');
        } else {
            $realip = getenv('REMOTE_ADDR');
        }
    }

    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty ($onlineip [0]) ? $onlineip [0] : '0.0.0.0';
    return $realip;
}

if (isset($_POST['Order'])) {
    $order = $_POST['Order'];
    if(isset($_POST['bank_type'])){
        $parameter = array(
            "version" => trim($config['version']),
            "charset" => trim($config['charset']),
            "signMethod" => 'MD5',
            "transType" => trim($config['trans_type']),
            "merId" => trim($config['mer_id']),
            "merCode" => trim($config['mer_code']),
            "backEndUrl" => trim($config['benotify']),
            "frontEndUrl"=>trim($config['ftnotify']),
            'orderTime'=>date('Y-m-d H:i:s', time()),
            'orderNumber'=>trim($order['orderNumber']), //genebackEndUrlrate order id, must be unqiue.
            'commodityName'=>trim($order['commodityName']),
            'commodityUrl'=>trim($order['commodityUrl']),
            'commodityUnitPrice'=>trim($order['commodityUnitPrice']),
            'commodityQuantity'=>trim($order['commodityQuantity']),
            'transferFee'=>trim($order['transferFee']),
            'commodityDiscount'=>trim($order['commodityDiscount']),
            'orderAmount'=>trim($order['orderAmount']),
            'orderCurrency'=> trim($config['mer_currency_code']),
            'customerName'=>trim($order['customerName']),
            'cutomerCardNumber'=>trim($order['cutomerCardNumber']),
            'bankNumber'=>$_POST['bank_type'],
            'transTimeout'=>trim($config['trans_timeout']),
            'customerIp'=>getCustormerIpaddress(),
            'origQid'=>'',
            'merReserved'=>''
        );
        $Submit = new Submit();
        $result = $Submit->getMsg($parameter, $config['purchase'], $config);
        if ($result) {
            if ($result->{'respCode'} == "00") { //no error;
                if ($result->{'formData'}) {
                    $params = $Submit->buildForm($result->{'formData'}, $result->{'url'}, "post", $config);
                    echo $params;
                    exit;
                } else {
                    $content = 'Malformed message';
                }
            } else {
                $content = $result->{'respMsg'};
            }
        } else {
            $content = 'request timesout';
        }
    }else
        $content = realpath(ROOT . 'views' . DIRECTORY_SEPARATOR . 'banklist.php');
    //defined layout
    $layout = realpath(ROOT . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '2columns_2.php');
    //include output template
    include_once realpath(ROOT . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . 'main.php');
}
?>
